<?php
namespace Mobile\Controller;
use Think\Controller;
class IndexController extends Controller 
{
	//手机首页
    public function index()
    {
        //首页显示城市
        $city = I('get.newcity')?I('get.newcity'):'广州';
        $this->assign('city',$city);

        //授课城市显示
        $citys = M('city')->where('parent_id=0')->select();
        $this->assign('citys',$citys);

        //判断是老师还是学员 显示昵称
        $id = $_SESSION['id'];
        $is_who = $_SESSION['is_who'];
        if ($is_who == 'laoshi') {
            $name = M('laoshi_user')->where(array('id'=>$id))->getField('name');
            $this->assign('name',$name);
        }else {
            $name = M('jiazhang_user')->where(array('id'=>$id))->getField('name');
            $this->assign('name',$name);
        }

        //高中名师 自动获取城市
        $sql = "select t1.id,t1.name,t1.city,t1.face,t2.jiaoling,t3.subject,t3.price from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t3.grades REGEXP '1|2|3|4' and t1.city='{$city}' order by t1.create_time desc limit 2";
        $gz = M()->query($sql);
        $this->assign('gz',$gz);

        //初中名师 自动获取城市
        $sql = "select t1.id,t1.name,t1.city,t1.face,t2.jiaoling,t3.subject,t3.price from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t3.grades REGEXP '5|6|7|8' and t1.city='{$city}' order by t1.create_time desc limit 2";
        $cz = M()->query($sql);
        $this->assign('cz',$cz);

        //小学名师 自动获取城市
        $sql = "select t1.id,t1.name,t1.city,t1.face,t2.jiaoling,t3.subject,t3.price from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t3.grades REGEXP '9|10|11|12|13|14|15' and t1.city='{$city}' order by t1.create_time desc limit 2";
        $xx = M()->query($sql);
        $this->assign('xx',$xx);

        // 特色课程/艺术名师 自动获取城市
        $sql = "select t1.id,t1.name,t1.city,t1.face,t2.jiaoling,t3.subject,t3.price from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where is_shenhe_xs=1 and is_dongjie=1 and is_del=0 and t3.grades like('%48%') and t1.city='{$city}' order by t1.create_time desc limit 2";
        $ys = M()->query($sql);
        $this->assign('ys',$ys);

        $this->display();
    }




    //老师个人详细信息
    public function ls_one()
    {
        $id = I('get.id');
        //首页显示城市
        $city = I('get.newcity')?I('get.newcity'):'广州';
        $this->assign('city',$city);

        //老师个人信息
        $sql = "select t1.id,t1.name,t1.face,t2.start_year,t2.start_month,t2.stop_year,t2.stop_month,t2.jiaoling,t2.jianjie,t2.experience,t2.stu_stye,t2.laoshi_stye,t3.subject,t3.address,t3.price,t3.grades,t3.methods from laoshi_user as t1 inner join jiaoling_jli as t2 on t1.id=t2.uid inner join showke_info as t3 on t1.id=t3.uid where t1.id='{$id}'";
        $laoshi_info = M()->query($sql);
        $this->assign('laoshi_info',current($laoshi_info));
    	

        //授课年级 跟上面字段(1,2,3,4) 匹配对应ID跟name显示
        $grade_ones = M('grade')->where('parent_id=0')->select();
        $this->assign('grade_ones',$grade_ones);

        //授课方式 跟上面字段(1,2,3,4) 匹配对应ID跟name显示
        $method_ones = M('teachingmethod')->select();
        $this->assign('method_ones',$method_ones);

        //成功案例
        $cases = M('success_case')->where(array('uid'=>$id))->select();
        $this->assign('cases',$cases);

        $this->display();
    }

    //家长向平台针对老师提出试课 提交
    public function shike()
    {
        //判断家长是否注册
        if ($_SESSION['is_who'] != 'jiazhang' ) {
            $this->redirect('mobile/user/login');
            exit;
        }

        $id = I('get.id');
        //首页显示城市
        $city = I('get.newcity')?I('get.newcity'):'广州';
        $cityid = M('city')->where(array('city_name'=>$city))->getField('cityid');
        $city_quyu = M('city')->where(array('parent_id'=>$cityid))->select();
        $this->assign('city_quyu',$city_quyu);

        //老师信息 
        $sql = "select t1.id,t1.name,t1.face,t1.city,t2.price,t2.subject,t2.grades from laoshi_user as t1 inner join showke_info as t2 on t1.id=t2.uid where t1.id='{$id}'";
        $laoshi = M()->query($sql);
        $this->assign('laoshi', current($laoshi));

        //授课年级 跟上面字段(1,2,3,4) 匹配对应ID跟name显示
        $grade_ones = M('grade')->where('parent_id=0')->select();
        $this->assign('grade_ones',$grade_ones);

        $this->display();
    }

    //
    public function shike_add()
    {   //Array ( [date] => 1 [time] => 上午 [address] => 1111111 [ls_uid] => 10 [subject] => 物理 )
        if (IS_POST) {
            $data['date'] = date('Y-m').'-'.I('post.date');
            $data['time'] = I('post.time');
            $data['jz_uid'] = $_SESSION['id'];
            $data['ls_uid'] = I('post.ls_uid');
            $data['city_quyu'] = I('post.address');
            $data['subject'] = I('post.subject');

            //入库
            $res = M('jz_to_ls_yuyue')->add($data);
            if ($res) {
               $this->redirect('mobile/info/jz_my_yuyue');
            }
        }
    }


    //首页 匿名找老师
    public function zhao_laoshi()
    {
        //首页显示城市
        $city = I('get.newcity')?I('get.newcity'):'广州';
        $this->assign('city',$city);

         //年级
        $grades = M('grade')->where(array('parent_id'=>0))->select();
        $this->assign('grades',$grades);
        //科目
        $kemus = M('subjects')->select();
        $this->assign('kemus',$kemus);  
             
        $this->display();
    }
    //添加提交
    public function zhao_laoshi_add()
    {
        if (IS_POST) {
            $data['name'] = I('post.name');

            $data['grade'] = I('post.grade');
            $data['beizhu'] = I('post.beizhu');

            $data['kemu'] = I('post.kemu');
            $data['addr'] = I('post.addr');
            $data['phone'] = I('post.phone');

            $send_to = I('post.send_to');

            $data['city'] = I('post.city');
            $data['create_time'] = time();

             //手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }

            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '短信码错误'
                    );
                echo json_encode($arr);
                exit;
            }

             //入库
             $res = M('zhao_laoshi')->add($data);
             if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '需求提交成功,我们课程顾问会第一时间联系您'
                    );
                echo json_encode($arr);
             }else {
                $arr = array(
                    'error' => 0,
                    'info' => '需求提交失败,请重新提交'
                    );
                echo json_encode($arr);
             }
        }
    }










}